home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / 68k Internet Config ƒ / C Source ƒ / IC Helper What.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  13.5 KB  |  551 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC Helper What.c
  3.     
  4. */
  5.  
  6. #include <LowMem.h>
  7. #include <Lists.h>
  8. #include <Icons.h>
  9.  
  10. #include "IC Window Globals.h"
  11. #include "IC Types.h"
  12. #include "IC Keys.h"
  13. #include "IC API.h"
  14. #include "IC Subs.h"
  15. #include "IC Dialogs.h"
  16. #include "IC Misc Subs.h"
  17. #include "IC Document.h"
  18. #include "IC Globals.h"
  19. #include "IC IconSuites.h"
  20. #include "IC StandardFile.h"
  21. #include "IC Movable Modal.h"
  22. #include "IC Windows.h"
  23.  
  24. #include "IC Helper What.h"
  25.  
  26. void GetNthElement(Handle entries,short index,InternalAppSpec* res){
  27.     BlockMoveData((Ptr)(((long)(*entries))+((index-1)*sizeof(InternalAppSpec))),res,sizeof(InternalAppSpec));
  28. }
  29.  
  30. void SetNthElement(Handle entries,short index,InternalAppSpec* res){
  31.     BlockMoveData((Ptr)res,(Ptr)(((long)(*entries))+((index-1)*sizeof(InternalAppSpec))),sizeof(InternalAppSpec));
  32. }
  33.  
  34. Boolean EntryLocked(Handle entries,short index){
  35.     InternalAppSpec appspec;
  36.     
  37.     GetNthElement(entries,index,&appspec);
  38.     return appspec.locked;
  39. }
  40.  
  41. void DimHelperButtons(WindowType wt){
  42.     DialogPtr dlg=WindowInfo[wt].window;
  43.     Handle entries=(Handle)WindowInfo[wt].items[ditHList]->data;
  44.     ListHandle lh=(ListHandle)WindowInfo[wt].items[ditHList]->spare_data;
  45.     short selection=SelectedLine(lh);
  46.     Boolean have_selection=(selection!=-1),elock=EntryLocked(entries,selection+1);
  47.     
  48.     SetDCtlEnable(dlg,ditHAdd,!(IsDocLocked()));
  49.     SetDCtlEnable(dlg,ditHDelete,(have_selection)&&(!elock));
  50.     SetDCtlEnable(dlg,ditHChange,(have_selection)&&(!elock));
  51.     OutlineDefault1(dlg,ditHMainUserItem);
  52. }
  53.  
  54. pascal void HelperUserItemUpdate(DialogPtr dlg,short item){
  55.     Rect list_rect;
  56.     
  57.     GetDItemRect(dlg,item,&list_rect);
  58.     PenNormal();
  59.     InsetRect(&list_rect,-1,-1);
  60.     FrameRect(&list_rect);
  61.     LUpdate(dlg->visRgn,(ListHandle)WindowInfo[GetWindowType(dlg)].items[item]->spare_data);
  62. }
  63.  
  64. void HelperLDDraw(Handle entries,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
  65.     InternalAppSpec appspec;
  66.     OSErr err;
  67.     Handle suite;
  68.     short cur_x;
  69.     Rect tmprect;
  70.     FontInfo sys_font_info;
  71.     short top,transfer;
  72.     RgnHandle rgn,rgn2;
  73.     Str255 ts1=kICHelper,ts2;
  74.     
  75.     if (datalen==0){
  76.         GetNthElement(entries,the_cell.v+1,&appspec);
  77.         EraseRect(r);
  78.         cur_x=0;
  79.         rgn=NewRgn();
  80.         if (System7){
  81.             tmprect.top=r->top;
  82.             tmprect.left=r->left;
  83.             tmprect.bottom=tmprect.top+32;
  84.             tmprect.right=tmprect.left+32;
  85.             
  86.             OffsetRect(&tmprect,((kCellHeight-32)/2),((kCellHeight-32)/2));
  87.             
  88.             GetDTDBIconSuiteCached('APPL',appspec.appspec.fCreator,&suite);
  89.             
  90.             if (suite!=(Handle)0){
  91.                 if (select)
  92.                     transfer=ttSelected;
  93.                 else
  94.                     transfer=ttNone;
  95.                 
  96.                 err=PlotIconSuite(&tmprect,atNone,transfer,suite);
  97.                 IconSuiteToRgn(rgn,&tmprect,atNone,suite);
  98.             }
  99.             cur_x+=kCellHeight;
  100.         }
  101.         cur_x+=2;
  102.         
  103.         TextFont(systemFont);
  104.         GetFontInfo(&sys_font_info);
  105.         
  106.         top=((kCellHeight-(sys_font_info.ascent+sys_font_info.descent))/2);
  107.         
  108.         TextFont(systemFont);
  109.         MoveTo(r->left+cur_x,r->top+top+sys_font_info.ascent);
  110.         
  111.         // ts1 is temp holder of kICHelper
  112.         // first copy the key to the appspec key
  113.         TPCopy(ts1,appspec.key,ts1[0]+1,255);
  114.         
  115.         // combine the key, " - ", and the app's name, then draw to right of icon element
  116.         GetAString(ts2,128,5);
  117.         
  118.         Concat(ts1,2,ts2,appspec.appspec.name);
  119.         
  120.         BlockMoveData((Ptr)&(ts2[1]),(Ptr)&(ts1[ts1[0]+1]),ts2[0]);
  121.         ts1[0]+=ts2[0];
  122.         BlockMoveData((Ptr)&(appspec.appspec.name[1]),(Ptr)&(ts1[ts1[0]+1]),appspec.appspec.name[0]);
  123.         ts1[0]+=appspec.appspec.name[0];
  124.         
  125.         DrawString(ts1);
  126.         
  127.         if (select){
  128.             rgn2=NewRgn();
  129.             RectRgn(rgn2,r);
  130.             if (Has_ColorQD){
  131.                 unsigned char hilitemode=LMGetHiliteMode();
  132.                 
  133.                 BitClr(&hilitemode,pHiliteBit);
  134.                 LMSetHiliteMode(hilitemode);
  135.             }
  136.             
  137.             XorRgn(rgn,rgn2,rgn2);
  138.             
  139.             InvertRgn(rgn2);
  140.             DisposeRgn(rgn2);
  141.         }
  142.         
  143.         if (rgn!=(RgnHandle)0)
  144.             DisposeRgn(rgn);
  145.     }
  146. }
  147.  
  148. pascal void HelperLDEF(short message,Boolean select,Rect* r,Cell the_cell,short offset,short datalen,ListHandle lh){
  149.     Handle entries;
  150.     
  151.     SetPort((*lh)->port);
  152.     entries=(Handle)WindowInfo[GetWindowType((*lh)->port)].items[ditHList]->data;
  153.     
  154.     switch (message){
  155.         case lInitMsg:
  156.             break;
  157.         case lDrawMsg:
  158.         case lHiliteMsg:
  159.             HelperLDDraw(entries,select,r,the_cell,offset,datalen,lh);
  160.             break;
  161.         case lCloseMsg:
  162.             break;
  163.         default:
  164.             break;
  165.     }
  166. }
  167.  
  168. OSErr WhatOpenHelper(WindowType wt,short item){
  169.     DialogPtr dlg;
  170.     Handle entries;
  171.     long attr;
  172.     OSErr err=noErr;
  173.     long count,ndx,size;
  174.     Rect list_rect,data_rect;
  175.     InternalAppSpec tmpapspec;
  176.     ListHandle lh;
  177.     Point cell_size;
  178.     Str255 tstr,ich=kICHelper;
  179.     
  180.     WindowInfo[wt].items[item]->spare_data=0;
  181.     WindowInfo[wt].items[item]->data=(Ptr)0;
  182.     dlg=WindowInfo[wt].window;
  183.     SetDItemHandle(dlg,item,(Handle)gHelperUserItemUpdate);
  184.     entries=NewHandle(0);
  185.     if (entries==(Handle)0)
  186.         err=memFullErr;
  187.     else {
  188.         ndx=1;
  189.         err=noErr;
  190.         do {
  191.             err=ICMapErr(ICGetIndPref(GetInstance(),ndx,tmpapspec.key));
  192.             if (err==noErr){
  193.                 // extract the first chars from the string to see if it begins with helper...
  194.                 TPCopy(tstr,tmpapspec.key,1,ich[0]);
  195.                 if (IUEqualString(tstr,ich)==0){
  196.                     size=sizeof(ICAppSpec);
  197.                     err=ICMapErr(ICGetPref(GetInstance(),tmpapspec.key,&attr,(Ptr)&(tmpapspec.appspec),&size));
  198.                     if (err==noErr){
  199.                         tmpapspec.locked=BitTst(&attr,ICattr_locked_bit)||IsDocLocked();
  200.                         err=PtrAndHand(&tmpapspec,entries,sizeof(InternalAppSpec));
  201.                     }
  202.                 }
  203.             }
  204.             ndx++;
  205.         } while (err==noErr);
  206.     }
  207.     
  208.     if (err==icPrefNotFoundErr)
  209.         err=noErr;
  210.     
  211.     if (err==noErr){
  212.         count=GetHandleSize(entries)/sizeof(InternalAppSpec);
  213.         WindowInfo[wt].items[item]->data=(Ptr)entries;
  214.         GetDItemRect(dlg,item,&list_rect);
  215.         list_rect.right-=15;
  216.         SetRect(&data_rect,0,0,1,count);
  217.         SetPt(&cell_size,list_rect.right-list_rect.left,kCellHeight);
  218.         lh=LNew(&list_rect,&data_rect,cell_size,128,dlg,true,false,false,true);
  219.         if (lh==(ListHandle)0)
  220.             err=memFullErr;
  221.     }
  222.     
  223.     if (err==noErr){
  224.         (*lh)->refCon=(long)gHelperLDEF;
  225.         (*lh)->selFlags=lOnlyOne;
  226.         WindowInfo[wt].items[item]->spare_data=(Ptr)lh;
  227.         DimHelperButtons(wt);
  228.         SetUpDefaultOutline(dlg,ditHChange,ditHMainUserItem);
  229.         WindowInfo[wt].selected_item=ditHList;
  230.     }
  231.     
  232.     return err;
  233. }
  234.  
  235. Boolean ChooseApplication(ICAppSpec* spec){
  236.     FInfo info;
  237.     FSSpec fs;
  238.     OSErr err;
  239.     
  240.     err=ICStandardGetFile('APPL',&fs,&info);
  241.     DisplayError(acChooseApplication,err);
  242.     if (err==noErr){
  243.         spec->fCreator=info.fdCreator;
  244.         SetPString(spec->name,1,fs.name);
  245.     }
  246.     
  247.     return err;
  248. }
  249.  
  250. void FixButton(DialogPtr dlg){
  251.     Str255 t1,t2;
  252.     
  253.     GetItemText(dlg,ditHAppName,t1);
  254.     GetItemText(dlg,ditHHelper,t2);
  255.     
  256.     SetDCtlEnable(dlg,ditHOK,((t1[0]!=0)&&(t2[0]!=0)));
  257.     OutlineDefault1(dlg,ditHAddUserItem);
  258. }
  259.  
  260. pascal Boolean DoAddFilter(DialogPtr dlg,EventRecord* er,short* item){
  261.     Boolean res=CancelModalFilter(dlg,er,item);
  262.     
  263.     FixButton(dlg);
  264.     
  265.     return res;
  266. }
  267.  
  268. /*
  269.     DoEdit
  270.     
  271.     Pops up a dialog to allow someone to choose a new application or change a helper string.
  272. */
  273. OSErr DoEdit(Handle entries,ListHandle lh,InternalAppSpec* choosen_app){
  274.     DialogPtr dlg=GetNewDialog(800,(Ptr)0,(WindowPtr)-1);
  275.     short item;
  276.     OSErr err=noErr;
  277.     Ptr saved_state;
  278.     
  279.     if (dlg==(DialogPtr)0)
  280.         err=memFullErr;
  281.     else {
  282.         SetUpDefaultOutline(dlg,ditHOK,ditHAddUserItem);
  283.         SetItemText(dlg,ditHHelper,choosen_app->key);
  284.         SetItemText(dlg,ditHAppName,choosen_app->appspec.name);
  285.         SelectDialogItemText(dlg,ditHHelper,0,255);
  286.         FixButton(dlg);
  287.         ShowWindow(dlg);
  288.         DisableMenuBar(&saved_state,-1);
  289.         InitCursor();
  290.         
  291.         do {
  292.             MovableModalDialog(gDoAddFilter,&item);
  293.             
  294.             if (item==ditHChooseApplication){
  295.                 // if we choose an application with no errors, fix up the dialog
  296.                 if (ChooseApplication(&(choosen_app->appspec))==noErr){
  297.                     SetItemText(dlg,ditHAppName,choosen_app->appspec.name);
  298.                     FixButton(dlg);
  299.                 }
  300.             }
  301.         } while ((item!=ditHOK)&&(item!=ditHCancel));
  302.         
  303.         if (item==ditHOK){
  304.             Str255 tstr;
  305.             
  306.             GetItemText(dlg,ditHHelper,tstr);
  307.             SetPString(choosen_app->key,2,kICHelper,tstr);
  308.         } else 
  309.             err=userCanceledErr;
  310.         
  311.         ReEnableMenuBar(&saved_state);
  312.         DisposeDialog(dlg);
  313.     }
  314.     return err;
  315. }
  316.  
  317. OSErr DoAdd(Handle entries,ListHandle lh){
  318.     InternalAppSpec choosen_app;
  319.     OSErr err;
  320.     
  321.     SetPString(choosen_app.appspec.name,0);
  322.     choosen_app.locked=false;
  323.     SetPString(choosen_app.key,0);
  324.     
  325.     err=DoEdit(entries,lh,&choosen_app);
  326.     
  327.     if (err==noErr){
  328.         err=PtrAndHand(&choosen_app,entries,sizeof(InternalAppSpec));
  329.         if (err==noErr)
  330.             LAddRow(1,32767,lh);
  331.     }
  332.     
  333.     return err;
  334. }
  335.  
  336. OSErr DoChange(short selection,Handle entries,ListHandle lh){
  337.     OSErr err;
  338.     InternalAppSpec tmpspec;
  339.     Str255 tmp=kICHelper;
  340.     
  341.     GetNthElement(entries,selection+1,&tmpspec);
  342.     Delete(tmpspec.key,1,tmp[0]);
  343.     
  344.     err=DoEdit(entries,lh,&tmpspec);
  345.     if (err==noErr)
  346.         SetNthElement(entries,selection+1,&tmpspec);
  347.     else
  348.         err=userCanceledErr;
  349.     
  350.     return err;
  351. }
  352.  
  353. /*
  354.     Oops, small bug in the pascal version, corrected here.  When you add a new
  355.     helper, the actual add to the prefs file is delayed until the window is flushed.
  356.     
  357.     However, deletes from the prefs file are done when the button is pressed.
  358.     
  359.     The bug occurs when you want to delete an entry which has just been added but
  360.     not saved (I don't know of a logical situation when this could occur, but it is possible).
  361.     The removal from the list only takes place if the removal from the prefs file is
  362.     successful.  The two should be logically separated in some way.  I chose to ignore
  363.     the 'resource not found' error and just remove it from the list.
  364. */
  365. OSErr DoDelete(short selection,Handle entries,ListHandle lh){
  366.     OSErr err,err2;
  367.     InternalAppSpec apspec;
  368.     long pos;
  369.     
  370.     err=ICMapErr(ICBegin(GetInstance(),icReadWritePerm));
  371.     if (err==noErr){
  372.         GetNthElement(entries,selection+1,&apspec);
  373.         err=ICMapErr(ICDeletePref(GetInstance(),apspec.key));
  374.         ICEnd(GetInstance());
  375.     }
  376.     if ((err==noErr)||(err==icPrefNotFoundErr)){
  377.         Munger(entries,selection*sizeof(InternalAppSpec),(Ptr)0,sizeof(InternalAppSpec),(Ptr)-1,0);
  378.         err=MemError();
  379.         if (err==noErr)
  380.             LDelRow(1,selection,lh);
  381.     }
  382.     
  383.     return err;
  384. }
  385.  
  386.  
  387. OSErr WhatClickHelper(WindowType wt,short item,EventRecord* er){
  388.     ListHandle lh=(ListHandle)WindowInfo[wt].items[ditHList]->spare_data;
  389.     Handle entries=(Handle)WindowInfo[wt].items[ditHList]->data;
  390.     short selection=SelectedLine(lh);
  391.     OSErr err=(OSErr)1;
  392.     
  393.     GlobalToLocal(&er->where);
  394.     
  395.     switch (item){
  396.         case ditHAdd:
  397.             err=DoAdd(entries,lh);
  398.             break;
  399.         case ditHChange:
  400.             err=DoChange(selection,entries,lh);
  401.             break;
  402.         case ditHDelete:
  403.             if (selection!=-1)
  404.                 err=DoDelete(selection,entries,lh);
  405.             break;
  406.         case ditHList:
  407.             if (LClick(er->where,0,lh)){
  408.                 selection=SelectedLine(lh);//selection may have been changed by LClick
  409.                 if (EntryLocked(entries,selection+1)){
  410.                     LockedAlert(wt,item);
  411.                     err=userCanceledErr;
  412.                 } else {
  413.                     FlashItem(WindowInfo[wt].window,ditHChange);
  414.                     err=DoChange(selection,entries,lh);
  415.                 }
  416.             }
  417.             break;
  418.         default:
  419.             break;
  420.     }
  421.     
  422.     if (err==noErr)
  423.         DirtyDocument();
  424.     
  425.     if (err==(OSErr)1)
  426.         err=noErr;
  427.     
  428.     DimHelperButtons(wt);
  429.     
  430.     return err;
  431. }
  432.  
  433. OSErr WhatFlushHelper(WindowType wt,short item){
  434.     OSErr err=noErr,first_err=noErr;
  435.     Handle entries=(Handle)WindowInfo[wt].items[item]->data;
  436.     short i;
  437.     InternalAppSpec appspec;
  438.     
  439.     for (i=1;i<(GetHandleSize(entries)/sizeof(InternalAppSpec));i++){
  440.         GetNthElement(entries,i,&appspec);
  441.         if (!appspec.locked)
  442.             err=ICMapErr(ICSetPref(GetInstance(),appspec.key,ICattr_no_change,(Ptr)&(appspec.appspec),sizeof(ICAppSpec)));
  443.         
  444.         if (err!=noErr)
  445.             if (first_err==noErr)
  446.                 first_err=err;
  447.     }
  448.     return first_err;
  449. }
  450.  
  451. OSErr WhatCloseHelper(WindowType wt,short item){
  452.     LDispose((ListHandle)WindowInfo[wt].items[item]->spare_data);
  453.     DisposeHandle((Handle)WindowInfo[wt].items[item]->data);
  454.     return noErr;
  455. }
  456.  
  457. OSErr WhatActivateHelper(WindowType wt,short item,Boolean activate){
  458.     LActivate(activate,(ListHandle)WindowInfo[wt].items[item]->spare_data);
  459.     return noErr;
  460. }
  461.  
  462. pascal StringPtr GetHelpEntryName(ListHandle list,Cell c,StringPtr str){
  463.     InternalAppSpec appspec;
  464.     Handle entries=(Handle)WindowInfo[GetWindowType((*list)->port)].items[ditHList]->data;
  465.     Str255 tstr,ich=kICHelper,astr;
  466.     
  467.     GetNthElement(entries,c.v+1,&appspec);
  468.     GetAString(astr,128,5);
  469.     
  470.     SetPString(str,3,TPCopy(tstr,appspec.key,ich[0]+1,255),astr,appspec.appspec.name);
  471.     
  472.     return str;
  473. }
  474.  
  475. OSErr WhatKeyHelper(WindowType wt,short item,EventRecord* er){
  476.     OSErr err=noErr;
  477.     char ch;
  478.     ListHandle lh;
  479.     Handle entries;
  480.     short selection;
  481.     Str255 tstr;
  482.     
  483.     if ((er->what==keyDown)||(er->what==autoKey)){
  484.         ch=er->message&0x00ff;
  485.         
  486.         switch (ch){
  487.             case crChar:
  488.             case enterChar:
  489.                 if (GetDCtlEnable(WindowInfo[wt].window,ditHChange)){
  490.                     entries=(Handle)WindowInfo[wt].items[ditHList]->data;
  491.                     lh=(ListHandle)WindowInfo[wt].items[item]->spare_data;
  492.                     selection=SelectedLine(lh);
  493.                     FlashItem(WindowInfo[wt].window,ditHChange);
  494.                     err=DoChange(selection,entries,lh);
  495.                     if (err==noErr)
  496.                         DirtyDocument();
  497.                 }
  498.                 break;
  499.             default:
  500.                 DoListKey((ListHandle)WindowInfo[wt].items[item]->spare_data,er,gGetHelpEntryName);
  501.                 DimHelperButtons(wt);
  502.                 break;
  503.         }
  504.     }
  505.     
  506.     return err;
  507. }
  508.  
  509. OSErr WhatCursorHelper(WindowType wt,short item,Point pt,short cursorid){
  510.     Rect r;
  511.     CursHandle curs;
  512.     
  513.     GetDItemRect(WindowInfo[wt].window,item,&r);
  514.     InsetRect(&r,15,0);
  515.     
  516.     if (PtInRect(pt,&r)){
  517.         curs=GetCursor(cursorid);
  518.         SetCursor(*curs);
  519.     } else
  520.         InitCursor();
  521.     
  522.     return noErr;
  523. }
  524.  
  525. void HelperSetSelection(StringPtr key){
  526.     long i;
  527.     WindowPtr window;
  528.     Handle entries;
  529.     InternalAppSpec appspec;
  530.     ListHandle list;
  531.     Str255 tstr;
  532.     
  533.     SetPString(tstr,2,kICHelper,key);
  534.     window=GetWindowPtr(WT_Helper);
  535.     if (window!=(WindowPtr)0){
  536.         list=(ListHandle)WindowInfo[WT_Helper].items[ditHList]->spare_data;
  537.         entries=(Handle)WindowInfo[WT_Helper].items[ditHList]->data;
  538.         
  539.         for (i=1;i<=(GetHandleSize(entries)/sizeof(InternalAppSpec));i++){
  540.             GetNthElement(entries,i,&appspec);
  541.             if (IUEqualString(appspec.key,tstr)==0){
  542.                 LSetSingleSelection(list,i-1);
  543.                 DimHelperButtons(WT_Helper);
  544.                 
  545.                 if (GetDCtlEnable(window,ditHChange))
  546.                     DoChange(i-1,entries,list);
  547.             }
  548.         }
  549.     }
  550. }
  551.